/*
 * @Author: jingfuu@163.com
 * @Date: 2024-11-25 16:12:30
 * @LastEditTime: 2024-12-16 10:58:29
 * @Description: file content
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Inject,
  Logger,
  Param,
  Post,
  Put,
  Query,
  Res,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { PostService } from '../service/PostService';
import PostQueryParam from '../param/PostQueryParam';
import { PostDto } from '../dto/PostDto';
import { R } from 'src/common/R';
import PageResult from 'src/common/PageResult';
import { Response } from 'express';
import ServiceException from 'src/common/ServiceException';
import { PreAuthorize } from 'src/common/decorator/PreAuthorizeDecorator';
import { Log } from 'src/common/decorator/LogDecorator';
import BusinessType from 'src/common/enum/BusinessType';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-11-25 16:12:30
 * @LastEditTime: 2024-11-25 16:20:55
 * @Description: 岗位管理
 */
@ApiTags('岗位接口')
@Controller('system/post')
class PostController {
  private readonly logger = new Logger(PostController.name);

  @Inject()
  private postService: PostService;

  @PreAuthorize('system:post:list')
  @ApiOperation({ summary: '分页查询岗位列表' })
  @Get('/list')
  async findPostList(@Query() param: PostQueryParam): Promise<R<PostDto>> {
    this.logger.log('query role list start, param:' + JSON.stringify(param));
    const result: PageResult<PostDto> = await this.postService.list(param);
    return R.page(result);
  }

  @Log({ title: '岗位管理', businessType: BusinessType.DELETE })
  @PreAuthorize('system:post:remove')
  @ApiOperation({ summary: '删除岗位' })
  @Delete('/:id')
  async deletePost(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.postService.deletePost(Number.parseInt(item));
      }
    } else {
      await this.postService.deletePost(Number.parseInt(id));
    }
    return R.ok();
  }

  @PreAuthorize('system:post:query')
  @ApiOperation({ summary: '获取岗位详情' })
  @Get('/:id(\\d+)')
  async findPostDetail(@Param('id') id: number): Promise<R<PostDto>> {
    const result = await this.postService.detail(id);
    return R.ok(result);
  }

  @Log({ title: '岗位管理', businessType: BusinessType.UPDATE })
  @PreAuthorize('system:post:edit')
  @ApiOperation({ summary: '修改岗位' })
  @Put()
  async modifyPost(@Body() post: PostDto): Promise<R<any>> {
    await this.postService.updatePost(post);
    return R.ok();
  }

  @Log({ title: '岗位管理', businessType: BusinessType.INSERT })
  @PreAuthorize('system:post:add')
  @ApiOperation({ summary: '新增岗位' })
  @Post()
  async createPost(@Body() post: PostDto): Promise<R<any>> {
    await this.postService.createPost(post);
    return R.ok();
  }

  @Log({ title: '岗位管理', businessType: BusinessType.EXPORT })
  @PreAuthorize('system:post:export')
  @ApiOperation({ summary: '导出岗位' })
  @Post('/export')
  async exportRole(@Query() param: PostQueryParam, @Res() res: Response) {
    try {
      const buffer = await this.postService.exportPost(param);
      res.set({
        'Content-Type': 'application/vnd.ms-excel',
        'Content-Disposition': 'attachment; filename=post.xlsx',
      });
      res.send(buffer);
    } catch (error) {
      this.logger.error('角色列表导出失败', error.stack);
      throw new ServiceException('角色列表导出失败');
    }
  }
}
export default PostController;
